package week07

import "strings"

// 151. 翻转字符串里的单词
// https://leetcode-cn.com/problems/reverse-words-in-a-string/

func reverseWords(s string) string {
	var strs []string
	var temp []byte
	for i := range s {
		if s[i] == ' ' {
			if len(temp) > 0 {
				strs = append(strs, string(temp))
				temp = nil
			}
		} else {
			temp = append(temp, s[i])
		}
	}
	if len(temp) > 0 {
		strs = append(strs, string(temp))
	}

	for i := 0; i < len(strs)/2; i++ {
		strs[i], strs[len(strs)-i-1] = strs[len(strs)-i-1], strs[i]
	}
	return strings.Join(strs, " ")
}